package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.*
import com.common.app.R
import com.common.app.data.bean.AdInfoBean
import com.common.app.databinding.DialogCustomCommonAdBinding
import com.common.base.app.extras.OnImageLoadListener
import com.common.base.app.extras.load
import com.common.base.utils.NavigationUtil.hideNavigationBar
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ViewUtils

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：7/3/2023
 *
 * 描述：广告弹框
 *
 * 修订历史：
 *
 */
class CustomCommonAdDialog(context: Context) :
    Dialog(context, R.style.custom_dialog_sv), View.OnClickListener {
    private var binding: DialogCustomCommonAdBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnCustomCommonAdListener? = null

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
//        setDialogTheme()
        setCanceledOnTouchOutside(false)
        mContext = context
    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCustomCommonAdBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }
        binding?.dialogRoot?.setOnClickListener(this)
        binding?.closeImg?.setOnClickListener(this)
    }

    override fun onClick(view: View) {

        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        when (view) {
            binding?.dialogRoot->{}
            binding?.closeImg-> dismiss()
            binding?.adImg-> listener?.onCustomCommonAdClick()
        }
    }

    fun show(mData: AdInfoBean) {
        super.show()

        //设置弹框的高为屏幕的一半宽是屏幕的宽
        val mDialogLp = this.window?.attributes

        //设置宽度
        mDialogLp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.height = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.gravity = Gravity.CENTER //设置dialog 在布局中的位置
        this.window?.attributes = mDialogLp
        window?.setWindowAnimations(R.style.fadeShowStyle) //添加动画:
        window?.setBackgroundDrawable(ColorDrawable(mContext.getColor(R.color.translucent_black_65))) // android:windowBackground
        hideNavigationBar(window)
        playAdPicture(mData.ImgUrl)
    }


    override fun dismiss() {
        super.dismiss()
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnCustomCommonAdListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnCustomCommonAdListener {
        fun onCustomCommonAdClick()
        fun onCustomCommonAdPlayEnd()
    }

    private fun playAdPicture(mUrl: String) {

        //屏幕比例
//        binding?.adImg?.loadYX(
//            ImgUrlUtils.getWidthAutoScale(
//                ImgUrlUtils.TYPE_HW,
//                mUrl,
//                720, "webp"
//            ), placeholder = R.drawable.common_transparent_bg, listener = object :
//                OnImageLoadListener {
//                override fun onSuccess(bitmap: Drawable?) {
//                    //图片加载成功 设置为可见
//                    createCountDownTimer(countdownNum)
//                }
//
//                override fun onFail(msg: String) {
//                }
//            })

        binding?.adImg?.load(
            mUrl,24f,placeholder = R.drawable.common_transparent_bg, listener = object :
                OnImageLoadListener {
                override fun onSuccess(bitmap: Drawable?) {
                    //图片加载成功 设置为可见
                }

                override fun onFail(msg: String) {
                }
            })
    }
}